package com.quectel.cms.controller.scheduling;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.vo.scheduling.*;
import com.quectel.constant.core.scheduling.SchedulingAttendanceGroupConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupDto;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupPersonBindingDto;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupPersonWorkTimeConfigDto;
import com.quectel.core.module.scheduling.dto.SchedulingTimeConfigDto;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupPersonBindingService;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupPersonWorkTimeConfigService;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupService;
import com.quectel.core.module.scheduling.service.SchedulingTimeConfigService;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/05 13:24
 */
@RestController
@RequestMapping("schedulingAttendanceGroupPersonWorkTimeConfig")
@Tag(name = "SchedulingAttendanceGroupPersonWorkTimeConfigController", description = "考勤组绑定人员排班关系相关api")
public class SchedulingAttendanceGroupPersonWorkTimeConfigController extends BaseController {

    @DubboReference
    private SchedulingAttendanceGroupPersonWorkTimeConfigService schedulingAttendanceGroupPersonWorkTimeConfigService;
    @DubboReference
    private DeptPersonService deptPersonService;
    @DubboReference
    private SchedulingTimeConfigService schedulingTimeConfigService;
    @DubboReference
    private SchedulingAttendanceGroupService schedulingAttendanceGroupService;
    @DubboReference
    private SchedulingAttendanceGroupPersonBindingService groupPersonBindingService;

    @GetMapping("quertPersonWorkTimeByGroupIdAndDate")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:list")
    @Operation(summary = "根据考勤组id和月份查询当月考勤人员的工作时间")
    public Response<List<CheckDateWorkTimeResp>> quertPersonWorkTimeByGroupIdAndDate(
            @Parameter(description = "小区id") @RequestParam Long villageId,
            @Parameter(description = "考勤组id") @RequestParam(required = false) Long groupId,
            @Parameter(description = "日期 yyyy-MM-dd") @RequestParam String day
    ) {

        //查询列表数据
        Request request = Request.configParamsNotPage(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());

        List<CheckDateWorkTimeResp> resultList = new ArrayList();
        List<GroupPersonWorkShiftResp.TimeGroupPersonCls> recordList = new ArrayList<>();


        request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupPersonWorkTimeConfigDto::getDate), day);

        // 传入groupId 查找指定的
        if (groupId != null) {
            SchedulingAttendanceGroupDto schedulingAttendanceGroupDto = schedulingAttendanceGroupService.selectById(groupId);
            if (schedulingAttendanceGroupDto == null) {
                return Response.error("考勤组不存在，请联系管理员");
            }

            // 手动排班
            // 查询配置
            // 具体日期条件
            request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupPersonWorkTimeConfigDto::getSchedulingAttendanceGroupId), groupId);

            List<SchedulingAttendanceGroupPersonWorkTimeConfigDto> schedulingAttendanceGroupPersonWorkTimeConfigDtos =
                    schedulingAttendanceGroupPersonWorkTimeConfigService.queryListWithoutPadding(request.getParams());

            if (CollectionUtils.isEmpty(schedulingAttendanceGroupPersonWorkTimeConfigDtos)) {
                return Response.<List<CheckDateWorkTimeResp>>ok().wrap(resultList);
            }

            for (SchedulingAttendanceGroupPersonWorkTimeConfigDto groupPersonWorkTimeConfigDto : schedulingAttendanceGroupPersonWorkTimeConfigDtos) {
                // 这里查询出来都是人和时间配置的关系
                List<SchedulingTimeConfigDto> configDtos = JacksonUtils.parseArray(groupPersonWorkTimeConfigDto.getTimeConfigArr(), SchedulingTimeConfigDto.class);
                for (SchedulingTimeConfigDto timeConfigDto : configDtos) {
                    GroupPersonWorkShiftResp.TimeGroupPersonCls timeGroupPersonCls = new GroupPersonWorkShiftResp.TimeGroupPersonCls();
                    timeGroupPersonCls.setPersonId(groupPersonWorkTimeConfigDto.getPersonId());
                    timeGroupPersonCls.setGroupId(groupPersonWorkTimeConfigDto.getSchedulingAttendanceGroupId());
                    timeGroupPersonCls.setTimeConfigId(timeConfigDto.getId());
                    recordList.add(timeGroupPersonCls);
                }
            }
            resultList = handlTimeGroupPersonCls(recordList);
        }

        if (groupId == null) {
            // 查询小区下的考勤组
            // 手动排班
            request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupDto::getVillageId), villageId);
            request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupDto::getShiftSystemType), SchedulingAttendanceGroupConstants.SchedulingTypeEnum.SCHEDULING.getType());
            List<SchedulingAttendanceGroupDto> schedulingAttendanceGroupDtos = schedulingAttendanceGroupService.queryListWithoutPadding(request.getParams());

            // 非空判断
            if (CollectionUtils.isNotEmpty(schedulingAttendanceGroupDtos)) {
                // 遍历考勤组，匹配当天排班
                for (SchedulingAttendanceGroupDto schedulingAttendanceGroupDto : schedulingAttendanceGroupDtos) {
                    // 具体日期条件
                    request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupPersonWorkTimeConfigDto::getSchedulingAttendanceGroupId), schedulingAttendanceGroupDto.getId());

                    List<SchedulingAttendanceGroupPersonWorkTimeConfigDto> schedulingAttendanceGroupPersonWorkTimeConfigDtos =
                            schedulingAttendanceGroupPersonWorkTimeConfigService.queryListWithoutPadding(request.getParams());

                    if (CollectionUtils.isEmpty(schedulingAttendanceGroupPersonWorkTimeConfigDtos)) {
                        continue;
                    }

                    // 考勤人员时间记录
                    for (SchedulingAttendanceGroupPersonWorkTimeConfigDto groupPersonWorkTimeConfigDto : schedulingAttendanceGroupPersonWorkTimeConfigDtos) {
                        // 这里查询出来都是人和时间配置的关系
                        List<SchedulingTimeConfigDto> configDtos = JacksonUtils.parseArray(groupPersonWorkTimeConfigDto.getTimeConfigArr(), SchedulingTimeConfigDto.class);
                        for (SchedulingTimeConfigDto timeConfigDto : configDtos) {
                            GroupPersonWorkShiftResp.TimeGroupPersonCls timeGroupPersonCls = new GroupPersonWorkShiftResp.TimeGroupPersonCls();
                            timeGroupPersonCls.setPersonId(groupPersonWorkTimeConfigDto.getPersonId());
                            timeGroupPersonCls.setGroupId(groupPersonWorkTimeConfigDto.getSchedulingAttendanceGroupId());
                            timeGroupPersonCls.setTimeConfigId(timeConfigDto.getId());
                            recordList.add(timeGroupPersonCls);
                        }
                    }
                    resultList = handlTimeGroupPersonCls(recordList);
                }
            } else {
                resultList = new ArrayList<>();
            }
        }

        return Response.<List<CheckDateWorkTimeResp>>ok().wrap(resultList);
    }

    private List<CheckDateWorkTimeResp> handlTimeGroupPersonCls(List<GroupPersonWorkShiftResp.TimeGroupPersonCls> dataList) {

        // 按timeConfigId分组
        Map<Long, List<GroupPersonWorkShiftResp.TimeGroupPersonCls>> groupedData = dataList.stream()
                .collect(Collectors.groupingBy(GroupPersonWorkShiftResp.TimeGroupPersonCls::getTimeConfigId));

        List<CheckDateWorkTimeResp> result = new ArrayList<>();
        for (Long timeConfigId : groupedData.keySet()) {
            List<GroupPersonWorkShiftResp.TimeGroupPersonCls> groupData = groupedData.get(timeConfigId);
            List<CheckDateWorkTimeResp.Groupcls> groupList = groupData.stream()
                    .collect(Collectors.groupingBy(GroupPersonWorkShiftResp.TimeGroupPersonCls::getGroupId))
                    .entrySet().stream()
                    .map(entry -> {
                        CheckDateWorkTimeResp.Groupcls groupcls = new CheckDateWorkTimeResp.Groupcls();
                        SchedulingAttendanceGroupDto schedulingAttendanceGroupDto = schedulingAttendanceGroupService.selectCacheById(entry.getKey());
                        groupcls.setGroupId(entry.getKey());
                        groupcls.setGroupName(schedulingAttendanceGroupDto.getName());
                        groupcls.setPeople(entry.getValue().stream()
                                .map(data -> {
                                    CheckDateWorkTimeResp.Groupcls.Peoplecls peoplecls = new CheckDateWorkTimeResp.Groupcls.Peoplecls();
                                    DeptPersonDto deptPersonDto = deptPersonService.selectCacheById(data.getPersonId());
                                    peoplecls.setPersonId(data.getPersonId());
                                    peoplecls.setName(deptPersonDto.getName());
                                    return peoplecls;
                                }).collect(Collectors.toList()));
                        return groupcls;
                    }).collect(Collectors.toList());

            CheckDateWorkTimeResp checkDateWorkTimeResp = new CheckDateWorkTimeResp();
            checkDateWorkTimeResp.setTimeConfigId(timeConfigId);
            checkDateWorkTimeResp.setGroup(groupList);
            result.add(checkDateWorkTimeResp);
        }

        for (CheckDateWorkTimeResp checkDateWorkTimeResp : result) {
            SchedulingTimeConfigDto timeConfigDto = schedulingTimeConfigService.selectCacheById(checkDateWorkTimeResp.getTimeConfigId());
            checkDateWorkTimeResp.setName(timeConfigDto.getName());
            checkDateWorkTimeResp.setStart(timeConfigDto.getWorkTime());
            checkDateWorkTimeResp.setEnd(timeConfigDto.getClosingTime());
            checkDateWorkTimeResp.setPeriod(timeConfigDto.getWorkTime(), timeConfigDto.getClosingTime());
        }

        return result;
    }


    @PostMapping("clearDateConfig")
    @Operation(summary = "清空设置")
    public Response<Object> clearDateConfig(@RequestBody @Validated SchedulingAttendanceGroupPersonWorkTimeClearReq clearReq) {
        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(clearReq.getGroupId()));
        if (schedulingAttendanceGroupService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        // 遍历传入的数组，查询对应 组+人员+日期的记录
        List<SchedulingAttendanceGroupPersonWorkTimeClearReq.PersonTimeInfo> personTimeInfoList = clearReq.getPersonTimeInfoList();
        if (CollectionUtils.isEmpty(personTimeInfoList)) {
            return Response.error("传入选中数据不能为空");
        }

        List<Long> personWorkTimeConfigIdList = new ArrayList<>();

        for (SchedulingAttendanceGroupPersonWorkTimeClearReq.PersonTimeInfo personTimeInfo : personTimeInfoList) {

            SchedulingAttendanceGroupPersonWorkTimeConfigDto selectDbValue = schedulingAttendanceGroupPersonWorkTimeConfigService.selectOneByUkCols(
                    clearReq.getGroupId(),
                    personTimeInfo.getId(),
                    personTimeInfo.getDate());
            if (selectDbValue != null) {
                personWorkTimeConfigIdList.add(selectDbValue.getId());
            }
        }

        // 批量删除筛选出的记录
        if (CollectionUtils.isNotEmpty(personWorkTimeConfigIdList)) {
            Long[] idArray = personWorkTimeConfigIdList.toArray(new Long[0]);
            schedulingAttendanceGroupPersonWorkTimeConfigService.deleteBatch(idArray);
        }

        return Response.ok();

    }


    @PostMapping("savePersonWorkTime")
    @Operation(summary = "保存人员的排班时间")
    public Response<Object> savePersonWorkTime(@RequestBody @Validated SavePersonWorkTimeVo savePersonWorkTimeVo) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(savePersonWorkTimeVo.getGroupId()));
        if (schedulingAttendanceGroupService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        // 这里根据前端传过来的 人员，日期查询到具体的某一条记录
        // 先拼装插入的数据

        List<Long> timeConfigIds = savePersonWorkTimeVo.getTimeConfigIds();
        if (CollectionUtils.isEmpty(timeConfigIds)) {
            return Response.error("配置时间段不能为空");
        }

        List<SchedulingTimeConfigDto> timeConfigDtoList = new ArrayList<>();
        for (Long timeConfigId : timeConfigIds) {
            SchedulingTimeConfigDto schedulingTimeConfigDto = schedulingTimeConfigService.selectById(timeConfigId);
            if (schedulingTimeConfigDto != null) {
                timeConfigDtoList.add(schedulingTimeConfigDto);
            }
        }

        Long groupId = savePersonWorkTimeVo.getGroupId();

        List<SavePersonWorkTimeVo.PersonDateInfo> personDateInfos = savePersonWorkTimeVo.getPersonDateInfos();

        List<SchedulingAttendanceGroupPersonWorkTimeConfigDto> saveDtoList = new ArrayList<>();
        List<Long> personWorkTimeIds = new ArrayList<>();

        for (SavePersonWorkTimeVo.PersonDateInfo personDateInfo : personDateInfos) {

            // 查询到的配置id数组（schedulingAttendanceGroupPersonWorkTimeConfig）
            SchedulingAttendanceGroupPersonWorkTimeConfigDto personWorkTimeConfigDto =
                    schedulingAttendanceGroupPersonWorkTimeConfigService.selectOneByUkCols(
                            groupId,
                            personDateInfo.getId(),
                            personDateInfo.getDate());
            if (personWorkTimeConfigDto != null) {
                personWorkTimeIds.add(personWorkTimeConfigDto.getId());
            }

            SchedulingAttendanceGroupPersonWorkTimeConfigDto savely = new SchedulingAttendanceGroupPersonWorkTimeConfigDto();
            savely.setSchedulingAttendanceGroupId(groupId);
            savely.setPersonId(personDateInfo.getId());
            savely.setDate(personDateInfo.getDate());
            savely.setTimeConfigArr(JacksonUtils.toJsonString(timeConfigDtoList));
            saveDtoList.add(savely);
        }

        // service 删除配置id数组，然后保存配置
        schedulingAttendanceGroupPersonWorkTimeConfigService.savePersonWorkTime(saveDtoList, personWorkTimeIds);
        return Response.ok();
    }

    @GetMapping("quertPersonWorkTimeByGroupIdAndMonth")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:list")
    @Operation(summary = "根据考勤组id和月份查询当月考勤人员的工作时间")
    public Response<Page<SchedulingAttendanceGroupPersonWorkTimeResp>> quertPersonWorkTimeByGroupId(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize,
            @Parameter(description = "小区id") @RequestParam Long villageId,
            @Parameter(description = "考勤组id") @RequestParam(required = false) Long groupId,
            @Parameter(description = "开始时间") @RequestParam String startTime,
            @Parameter(description = "结束时间") @RequestParam String endTime) {

        //查询列表数据
        Request request = Request.configParamsNotPage(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());

        request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupPersonWorkTimeConfigDto::getSchedulingAttendanceGroupId), groupId);

        // 查询考勤组绑定的人员关系表
        int groupBindPersonCount = groupPersonBindingService.queryTotal(request.getParams());
        List<SchedulingAttendanceGroupPersonWorkTimeResp> resultList = new ArrayList<>();

        if (groupBindPersonCount > 0) {
            List<SchedulingAttendanceGroupPersonBindingDto> groupPersonBindingDtos =
                    groupPersonBindingService.queryList(request.getParams());

            // 查询条件填充 时间条件
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft(), startTime);
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getRight(), endTime);

            for (SchedulingAttendanceGroupPersonBindingDto groupPersonBindingDto : groupPersonBindingDtos) {
                request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupPersonWorkTimeConfigDto::getPersonId), groupPersonBindingDto.getPersonId());

                SchedulingAttendanceGroupPersonWorkTimeResp resp = new SchedulingAttendanceGroupPersonWorkTimeResp();

                Long personId = groupPersonBindingDto.getPersonId();
                // 1.填充人员信息
                DeptPersonDto deptPersonDto = deptPersonService.selectCacheById(personId);
                if (deptPersonDto != null) {
                    resp.setId(personId);
                    resp.setName(deptPersonDto.getName());

                    // 2.根据人员id查询绑定时间段
                    request.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupPersonWorkTimeConfigDto::getPersonId), personId);
                    List<SchedulingAttendanceGroupPersonWorkTimeConfigDto> personWorkTimeConfigDtos
                            = schedulingAttendanceGroupPersonWorkTimeConfigService.queryList(request.getParams());

                    List<SchedulingAttendanceGroupPersonWorkTimeResp.DateWorkTimeConfig> dateWorkTimeConfigList = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(personWorkTimeConfigDtos)) {
                        for (SchedulingAttendanceGroupPersonWorkTimeConfigDto personWorkTimeConfigDto
                                : personWorkTimeConfigDtos) {
                            SchedulingAttendanceGroupPersonWorkTimeResp.DateWorkTimeConfig dateWorkTimeConfig =
                                    new SchedulingAttendanceGroupPersonWorkTimeResp.DateWorkTimeConfig();
                            // 4.放入日期
                            dateWorkTimeConfig.setDate(personWorkTimeConfigDto.getDate());

                            List<SchedulingAttendanceGroupPersonWorkTimeResp.ExecInfo> execInfoList = new ArrayList<>();
                            // 5.放入日期的时间配置信息
                            String timeConfigArr = personWorkTimeConfigDto.getTimeConfigArr();
                            List<SchedulingTimeConfigDto> timeConfigIds = JacksonUtils.parseArray(timeConfigArr, SchedulingTimeConfigDto.class);
                            for (SchedulingTimeConfigDto timeConfig : timeConfigIds) {
                                SchedulingAttendanceGroupPersonWorkTimeResp.ExecInfo addly =
                                        new SchedulingAttendanceGroupPersonWorkTimeResp.ExecInfo();

                                SchedulingTimeConfigDto schedulingTimeConfigDto = schedulingTimeConfigService.selectById(timeConfig.getId());
                                if (schedulingTimeConfigDto != null) {
                                    addly.setTimeConfigId(schedulingTimeConfigDto.getId());
                                    addly.setName(schedulingTimeConfigDto.getName());
                                    addly.setWorkTime(schedulingTimeConfigDto.getWorkTime());
                                    addly.setClosingTime(schedulingTimeConfigDto.getClosingTime());
                                    execInfoList.add(addly);
                                }
                            }
                            dateWorkTimeConfig.setExecInfos(execInfoList);
                            dateWorkTimeConfigList.add(dateWorkTimeConfig);
                        }
                    }
                    resp.setGtArray(dateWorkTimeConfigList);
                }
                resultList.add(resp);
            }
        } else {
            resultList = new ArrayList<>();
        }

        Page<SchedulingAttendanceGroupPersonWorkTimeResp> page = new Page<>(request.getPageNo(), request.getPageSize(), groupBindPersonCount, resultList);
        return Response.<Page<SchedulingAttendanceGroupPersonWorkTimeResp>>ok().wrap(page);
    }


    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:list")
    @Operation(summary = "分页获取考勤组绑定人员排班关系列表")
    public Response<Page<SchedulingAttendanceGroupPersonWorkTimeConfigDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = schedulingAttendanceGroupPersonWorkTimeConfigService.queryTotal(request.getParams());

        List<SchedulingAttendanceGroupPersonWorkTimeConfigDto> list = null;
        if (total > 0) {
            list = schedulingAttendanceGroupPersonWorkTimeConfigService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<SchedulingAttendanceGroupPersonWorkTimeConfigDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<SchedulingAttendanceGroupPersonWorkTimeConfigDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:edit")
    @Operation(summary = "在编辑时根据id查询考勤组绑定人员排班关系详情")
    public Response<SchedulingAttendanceGroupPersonWorkTimeConfigDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (schedulingAttendanceGroupPersonWorkTimeConfigService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        SchedulingAttendanceGroupPersonWorkTimeConfigDto schedulingAttendanceGroupPersonWorkTimeConfigDto = schedulingAttendanceGroupPersonWorkTimeConfigService.selectById(id);

        return Response.<SchedulingAttendanceGroupPersonWorkTimeConfigDto>ok().wrap(schedulingAttendanceGroupPersonWorkTimeConfigDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:info")
    @Operation(summary = "根据id查询考勤组绑定人员排班关系的详情")
    public Response<SchedulingAttendanceGroupPersonWorkTimeConfigDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (schedulingAttendanceGroupPersonWorkTimeConfigService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        SchedulingAttendanceGroupPersonWorkTimeConfigDto schedulingAttendanceGroupPersonWorkTimeConfigDto = schedulingAttendanceGroupPersonWorkTimeConfigService.selectById(id);

        return Response.<SchedulingAttendanceGroupPersonWorkTimeConfigDto>ok().wrap(schedulingAttendanceGroupPersonWorkTimeConfigDto);
    }

    /**
     * 保存
     *
     * @param schedulingAttendanceGroupPersonWorkTimeConfigDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:edit")
    @Operation(summary = "新增考勤组绑定人员排班关系")
    public Response<Object> save(@RequestBody @Validated SchedulingAttendanceGroupPersonWorkTimeConfigDto schedulingAttendanceGroupPersonWorkTimeConfigDto) {

        //填充基础数据
        fillSaveCommonData(schedulingAttendanceGroupPersonWorkTimeConfigDto);

        schedulingAttendanceGroupPersonWorkTimeConfigService.save(schedulingAttendanceGroupPersonWorkTimeConfigDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param schedulingAttendanceGroupPersonWorkTimeConfigDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:edit")
    @Operation(summary = "根据id修改考勤组绑定人员排班关系")
    public Response<Object> update(@RequestBody @Validated SchedulingAttendanceGroupPersonWorkTimeConfigDto schedulingAttendanceGroupPersonWorkTimeConfigDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(schedulingAttendanceGroupPersonWorkTimeConfigDto.getId()));
        if (schedulingAttendanceGroupPersonWorkTimeConfigService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(schedulingAttendanceGroupPersonWorkTimeConfigDto);

        schedulingAttendanceGroupPersonWorkTimeConfigService.updateById(schedulingAttendanceGroupPersonWorkTimeConfigDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "schedulingAttendanceGroupPersonWorkTimeConfig:delete")
    @Operation(summary = "根据多条记录id批量删除考勤组绑定人员排班关系")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (schedulingAttendanceGroupPersonWorkTimeConfigService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        schedulingAttendanceGroupPersonWorkTimeConfigService.deleteBatch(ids);

        return Response.ok();
    }
}
